<% include ../header.ejs %>

<link rel="stylesheet" href="/stylesheets/app/recommendadd/recommendadd.css">

<header data-am-widget="header" data-am-sticky class="am-header am-header-default" id="appTemplates-Nav">
    <div class="am-header-left am-header-nav">
        <a href='javascript:history.go(-1)' class="">
            <img class="am-header-icon-custom" src="data:image/svg+xml;charset&#x3D;utf-8,&lt;svg xmlns&#x3D;&quot;http://www.w3.org/2000/svg&quot; viewBox&#x3D;&quot;0 0 12 20&quot;&gt;&lt;path d&#x3D;&quot;M10,0l2,2l-8,8l8,8l-2,2L0,10L10,0z&quot; fill&#x3D;&quot;%23fff&quot;/&gt;&lt;/svg&gt;" alt=""/>
        </a>
    </div>

    <h1 class="am-header-title">
        <a href="#title-link" class="" id="Typeface-whitecolor">修改密码</a>
    </h1>
</header>

<form class="am-form" id="doc-vld-msg">
    <fieldset  id="formFS">
        <div class="am-form-group">
            <label for="doc-vld-name">用户名：</label>
            <input type="text" class="" id="name" value="<%= user.name%>" required disabled>
        </div>

        <div class="am-form-group">
            <label for="doc-vld-name">旧密码(必填)：</label>
            <input type="password" class="" id="oldpassword" placeholder="密码" required>
        </div>

        <div class="am-form-group">
            <label for="doc-vld-name">新密码(必填)：</label>
            <input type="password" class="" id="newpassword" placeholder="新密码" required>
        </div>

        <div class="am-form-group">
            <label for="doc-vld-name">再次输入密码(必填)：</label>
            <input type="password" class="" id="newpassword2" placeholder="再次输入密码" required>
        </div>

        <button type="button" class="am-btn am-btn-primary am-btn-block" id="doc-save-toggle">确定</button>

    </fieldset>
</form>

<div class="am-modal am-modal-confirm" tabindex="-1" id="my-save">
    <div class="am-modal-dialog">
        <div class="am-modal-bd">
            是否确定更新密码
        </div>
        <div class="am-modal-footer">
            <span class="am-modal-btn" data-am-modal-cancel>取消</span>
            <span class="am-modal-btn" data-am-modal-confirm>确定</span>
        </div>
    </div>
</div>

<div class="am-modal am-modal-loading am-modal-no-btn" tabindex="-1" id="my-modal-loading">
    <div class="am-modal-dialog">
        <div class="am-modal-hd">正在载入...</div>
        <div class="am-modal-bd">
            <span class="am-icon-spinner am-icon-spin"></span>
        </div>
    </div>
</div>

<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert">
    <div class="am-modal-dialog">
        <div class="am-modal-bd" id ="alert-msg">
        </div>
        <div class="am-modal-footer">
            <span class="am-modal-btn">确定</span>
        </div>
    </div>
</div>



<script>
    //js表单验证
    $(function() {
        $('#doc-vld-msg').validator({
            onValid: function(validity) {
                $(validity.field).closest('.am-form-group').find('.am-alert').hide();
            },

            onInValid: function(validity) {
                var $field = $(validity.field);
                var $group = $field.closest('.am-form-group');
                var $alert = $group.find('.am-alert');
                // 使用自定义的提示信息 或 插件内置的提示信息
                var msg = $field.data('validationMessage') || this.getValidationMessage(validity);

                if (!$alert.length) {
                    $alert = $('<div class="am-alert am-alert-danger"></div>').hide().
                            appendTo($group);
                }

                $alert.html(msg).show();
            }
        });
    });

    //更新密码
    $(function() {
        $('#doc-save-toggle').on('click', function() {
            var formValidity =  $('#doc-vld-msg').validator('isFormValid')

            if(formValidity) {
                $('#my-save').modal({
                    relatedTarget: this,
                    onConfirm: function (options) {
                        if ($('#oldpassword').val() == "") {
                            alert("旧密码不能为空")
                            return
                        }

                        if ($('#newpassword').val() == "") {
                            alert("新密码不能为空")
                            return
                        }

                        if ($('#newpassword2').val() == "") {
                            alert("再次输入密码不能为空")
                            return
                        }

                        if ($('#newpassword2').val() != $('#newpassword').val() ) {
                            $('#alert-msg').html("两次输入的密码不一样")
                            $('#my-alert').modal('open')
                            return
                        }

                        $('#my-modal-loading').modal('open')

                        var options = {
                            url: 'pswrest',
                            type: 'post',
                            dataType: 'json',
                            data: {
                                'rowId': '<%= user.rowId%>',
                                'oldpassword': $('#oldpassword').val(),
                                'newpassword': $('#newpassword').val()
                            },
                            error: function (request) {
                                $('#my-modal-loading').modal('close')
                                $('#alert-msg').html("更新密码失败")
                                $('#my-alert').modal('open')
                                $('#doc-vld-msg').validator('destroy')
                            },
                            success: function (data) {
                                $('#my-modal-loading').modal('close')
                                if (data.isUpdate == 0) {
                                    $('#alert-msg').html("原密码输入错误")
                                    $('#my-alert').modal('open')
                                } else if(data.isUpdate == -1) {
                                    $('#alert-msg').html("更新密码失败")
                                    $('#my-alert').modal('open')
                                }else {
                                    location.href = "../login"
                                }
                                $('#doc-vld-msg').validator('destroy')
                            }
                        };
                        $.ajax(options);

                    },
                    // closeOnConfirm: false,
                    onCancel: function () {
                        $('#doc-vld-msg').validator('destroy')
                    }
                });
            }else {

            }

        });
    });



</script>


<% include ../footer.ejs %>